package com.taptech.doufu.bean.personalcenter;

import java.lang.reflect.Field;

import org.json.JSONArray;
import org.json.JSONObject;

import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.bean.ActionMsgBean;
import com.taptech.doufu.bean.BaseBean;
import com.taptech.doufu.bean.GroupBriefBean;
import com.taptech.doufu.bean.HomeTopBean;
import com.taptech.doufu.bean.NovelBean;
import com.taptech.doufu.bean.SweepBean;
import com.taptech.doufu.bean.UserBean;
import com.taptech.doufu.bean.CommentDataBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.GroupInfoBean;

@SuppressWarnings("serial")
public class PersonalDynamicBean extends BaseBean {
	private BaseBean content;
	private BaseBean target;
	private String uid;
	private String object_type;
	private String activity_id;
	private String op_type;
	private String add_time;
	private String content_type;
	private String target_type;
	private String target_id;
	private String topicId;
	private SweepBean sweep;
	private NovelBean novel;

	public SweepBean getSweep() {
		return sweep;
	}

	public void setSweep(SweepBean sweep) {
		this.sweep = sweep;
	}

	public NovelBean getNovel() {
		return novel;
	}

	public void setNovel(NovelBean novel) {
		this.novel = novel;
	}

	public BaseBean getContent() {
		return content;
	}

	public void setContent(BaseBean content) {
		this.content = content;
	}

	public BaseBean getTarget() {
		return target;
	}

	public void setTarget(BaseBean target) {
		this.target = target;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getObject_type() {
		return object_type;
	}

	public void setObject_type(String object_type) {
		this.object_type = object_type;
	}

	public String getActivity_id() {
		return activity_id;
	}

	public void setActivity_id(String activity_id) {
		this.activity_id = activity_id;
	}

	public String getOp_type() {
		return op_type;
	}

	public void setOp_type(String op_type) {
		this.op_type = op_type;
	}

	public String getAdd_time() {
		return add_time;
	}

	public void setAdd_time(String add_time) {
		this.add_time = add_time;
	}

	public String getContent_type() {
		return content_type;
	}

	public void setContent_type(String content_type) {
		this.content_type = content_type;
	}

	public String getTarget_type() {
		return target_type;
	}

	public void setTarget_type(String target_type) {
		this.target_type = target_type;
	}

	public String getTarget_id() {
		return target_id;
	}

	public void setTarget_id(String target_id) {
		this.target_id = target_id;
	}

	public String getTopicId() {
		return topicId;
	}

	public void setTopicId(String topicId) {
		this.topicId = topicId;
	}

	public void setJson(JSONObject json) {
		try {
			TTLog.s(json.toString(0));
			Field fields[] = getClass().getDeclaredFields();
			String fieldName;
			Object value = null;

			for (Field field : fields) {
				field.setAccessible(true);
				fieldName = field.getName();
				if (fieldName.equals("json"))
					continue;
				if (json.has(fieldName) && !json.isNull(fieldName)) {
					if (fieldName.equalsIgnoreCase("comuBrief")) {
						JSONObject comObj = json.getJSONObject(fieldName);
						GroupBriefBean group = new GroupBriefBean();
						group.setJson(comObj);
						value = group;
					} else if (fieldName.equalsIgnoreCase("community")) {
						JSONObject userObj = json.getJSONObject(fieldName);
						GroupInfoBean community = new GroupInfoBean();
						community.setJson(userObj);
						value = community;
					} else if (fieldName.equalsIgnoreCase("user")) {
						JSONObject userObj = json.getJSONObject(fieldName);
						UserBean user = new UserBean();
						user.setJson(userObj);
						value = user;
					} else if (fieldName.equalsIgnoreCase("to_user")) {
						JSONObject userObj = json.getJSONObject(fieldName);
						UserBean user = new UserBean();
						user.setJson(userObj);
						value = user;
					} else if (fieldName.equalsIgnoreCase("from_user")) {
						JSONObject userObj = json.getJSONObject(fieldName);
						UserBean user = new UserBean();
						user.setJson(userObj);
						value = user;
					} else if (fieldName.equalsIgnoreCase("content")) {
						int type = json.getInt(Constant.OP_TYPE);
						if (type == Constant.OP_TYPE_COMMENT) {
							JSONObject commentJson = json
									.getJSONObject(fieldName);
							CommentDataBean comment = new CommentDataBean();
							comment.setJson(commentJson);
							value = comment;
						} else if (type == Constant.OP_TYPE_UGC) {
							JSONObject ugcJson = json.getJSONObject(fieldName);
							HomeTopBean ugc = new HomeTopBean();
							ugc.setJson(ugcJson);
							value = ugc;
						}

					} else if (fieldName.equalsIgnoreCase("target")) {
						JSONObject jsonValue = json.getJSONObject(fieldName);
						HomeTopBean data = new HomeTopBean();
						data.setJson(jsonValue);
						value = data;

					} else if (fieldName.equalsIgnoreCase("sweep")) {
						JSONObject sweepObj = json.getJSONObject(fieldName);
						SweepBean sweep = new SweepBean();
						sweep.setJson(sweepObj);
						value = sweep;
					} else if (fieldName.equalsIgnoreCase("novel")) {
						JSONObject novelObj = json.getJSONObject(fieldName);
						NovelBean novel = new NovelBean();
						novel.setJson(novelObj);
						value = novel;
					} else if (fieldName.equalsIgnoreCase("actionMsg")) {
						JSONObject actionObj = json.getJSONObject(fieldName);
						ActionMsgBean action = new ActionMsgBean();
						action.setJson(actionObj);
						value = action;
					} else if (fieldName.equalsIgnoreCase("images")) {
						JSONArray jsonImages = json.getJSONArray(fieldName);
						value = initImages(jsonImages);
					} else if (fieldName.equalsIgnoreCase("result")) {
						JSONArray jsonResule = json.getJSONArray(fieldName);
						value = initResult(jsonResule);
					} else if (fieldName.equalsIgnoreCase("replies")) {
						JSONArray jsonResule = json.getJSONArray(fieldName);
						value = initReplies(jsonResule);
					} else {
						if (json.get(fieldName) instanceof String) {
							value = json.getString(fieldName);
						} else if (json.get(fieldName) instanceof Integer) {
							value = json.getString(fieldName);
						} else {
							value = "";
						}
					}
					field.set(this, value);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
